package ui.map 
{
	import com.gaiaframework.api.Gaia;
	import com.gaiaframework.api.IBitmapSprite;
	import com.gaiaframework.assets.SpriteAsset;
	import com.greensock.TweenMax;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import model.Model;
	import pages.Pages;
	import utils.ShapeUtil;
	import utils.TextFieldUtil;
	
	/**
	 * ...
	 * @author usual
	 */
	public class MapToolTip extends MovieClip 
	{
		private var _toolTipItem:BulleMarker;
		private var _spriteAsset:SpriteAsset;
		private var _containerAsset:MovieClip;
		private var _title:TextField;
		private var _colorTitle:Number;
		
		public function MapToolTip() 
		{
			addEventListener(Event.ADDED_TO_STAGE, build);
		}
		
		public function over(e:MouseEvent):void 
		{
			trace("[MapToolTip.over] ", e.currentTarget.name);
			reparentAsset(e.currentTarget.name);
			addTitle(e.currentTarget.name);
		}
		
		private function addTitle(assetName:String):void 
		{
			_title = new TextField();
			_title.selectable = false;
			_title.wordWrap = true;
			_title.multiline = true;
			_title.width = _toolTipItem.width - 50;
			_colorTitle = Number(Model.instance.getUiLabel(assetName + '-colorTitle'));
			TextFieldUtil.setText(_title, "<marker>" + Model.instance.getUiLabel(assetName + '-title') + "</marker>");
			_title.mouseEnabled = false;
			_containerAsset.addChild(_title);
			_title.x = -_title.width / 2;
			_title.y = int((_toolTipItem.bulle_mc.y) - (_title.height / 2));
			TweenMax.to(_title, 0, { tint:_colorTitle } );
			//_title.alpha = 0;
		}
		
		
		private function reparentAsset(assetName:String):void 
		{
			if (_containerAsset) removeChild(_containerAsset);
			_containerAsset = new MovieClip();
			_spriteAsset = SpriteAsset(Gaia.api.getPage(Pages.HOME).assets[assetName]);
			_spriteAsset.mouseEnabled = false;
			IBitmapSprite(_spriteAsset).smoothing = true;
			_spriteAsset.visible = true;
			//_spriteAsset.alpha = 0;
			_spriteAsset.scaleX = _spriteAsset.scaleY = 2;
			_spriteAsset.x = -_spriteAsset.width / 2;
			_spriteAsset.y =  -_spriteAsset.height/2;
			_containerAsset.addChild(_spriteAsset.container);
			TweenMax.to(_spriteAsset.container, 0, { colorTransform: { tint:0x000000, tintAmount:0.1 }} );
			var c:MaskMapMarker = new MaskMapMarker();
			c.cacheAsBitmap = true;
			c.scaleX =  c.scaleY = 0.21;
			c.x = -2;
			c.y = -189;
			_containerAsset.addChild(c);
			//TweenMax.to(c, 0, { tint:0xfff000 } );
			_spriteAsset.mask = c;		
			addChild(_containerAsset);
		}
		
		private function build(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, build);
			addToolTip();
		}
		
		private function addToolTip():void 
		{
			_toolTipItem = new BulleMarker();
			addChild(_toolTipItem);
			_toolTipItem.bulle_mc.scaleX = _toolTipItem.bulle_mc.scaleY = 0.21;
			_toolTipItem.bulle_mc.x = 0;
			_toolTipItem.bulle_mc.y = -_toolTipItem.bulle_mc.height/2;
			//_toolTipItem.x = -_toolTipItem.bulle_mc.width / 2;
			//_toolTipItem.y = -_toolTipItem.bulle_mc.height/2;
		}
		
	}

}